package com.crawler.ui.control;

import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import com.crawler.commons.service.MissionService;
import com.crawler.util.StringUtil;

public class Main {

	protected Shell shlAsp;
	private Table missionTable;
	private Table table_1;
	private Table table_pageAction;
	private TabItem tbtext;
	private Table table_init;
	private Text text_accessInterval;
	private Text text_cycleLength;
	private Text text_cycleCount;
	private Text text_slidingWindowSize;
	Display display;
	TestShell shell = new TestShell(display);
	Combo combo_iflog;
	Combo combo_yxj;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Main window = new Main();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		display = Display.getDefault();
		createContents();
		initService();
		shlAsp.open();
		shlAsp.layout();
		while (!shlAsp.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shlAsp = new Shell();
		shlAsp.setSize(1107, 658);
		shlAsp.setText("aspriator");
		TabFolder tabFolder = new TabFolder(shlAsp, SWT.NONE);
		tabFolder.setBounds(269, 75, 812, 534);

		TabItem tbinit = new TabItem(tabFolder, SWT.NONE);
		tbinit.setText("初始入口");

		Composite composite_1 = new Composite(tabFolder, SWT.NONE);
		tbinit.setControl(composite_1);

		table_init = new Table(composite_1, SWT.BORDER | SWT.FULL_SELECTION);
		table_init.setBounds(0, 0, 804, 443);
		table_init.setHeaderVisible(true);
		table_init.setLinesVisible(true);

		TableColumn tableColumn_5 = new TableColumn(table_init, SWT.NONE);
		tableColumn_5.setWidth(100);
		tableColumn_5.setText("序号");

		TableColumn tableColumn_6 = new TableColumn(table_init, SWT.NONE);
		tableColumn_6.setWidth(100);
		tableColumn_6.setText("类别");

		TableColumn tblclmnUrl = new TableColumn(table_init, SWT.NONE);
		tblclmnUrl.setWidth(100);
		tblclmnUrl.setText("URL");

		TableColumn tableColumn_7 = new TableColumn(table_init, SWT.NONE);
		tableColumn_7.setWidth(100);
		tableColumn_7.setText("查询");

		Button btnNewButton = new Button(composite_1, SWT.NONE);
		btnNewButton.setBounds(714, 467, 80, 27);
		btnNewButton.setText("New Button");

		Button btnNewButton_1 = new Button(composite_1, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				try {
					shell.open();
					shell.layout();
					while (!shell.isDisposed()) {
						if (!display.readAndDispatch()) {
							display.sleep();
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		btnNewButton_1.setBounds(628, 467, 80, 27);
		btnNewButton_1.setText("New Button");

		TabItem tbPageAction = new TabItem(tabFolder, SWT.NONE);
		tbPageAction.setText("页面动作");

		Composite composite = new Composite(tabFolder, SWT.NONE);
		tbPageAction.setControl(composite);

		table_pageAction = new Table(composite, SWT.BORDER | SWT.FULL_SELECTION);
		table_pageAction.setBounds(0, 0, 804, 454);
		table_pageAction.setHeaderVisible(true);
		table_pageAction.setLinesVisible(true);

		TableColumn tblclmnId = new TableColumn(table_pageAction, SWT.NONE);
		tblclmnId.setWidth(37);
		tblclmnId.setText("ID");

		TableColumn tblclmnNewColumn_6 = new TableColumn(table_pageAction, SWT.NONE);
		tblclmnNewColumn_6.setWidth(100);
		tblclmnNewColumn_6.setText("规则动作名");

		TableColumn tableColumn = new TableColumn(table_pageAction, SWT.NONE);
		tableColumn.setWidth(100);
		tableColumn.setText("动作类");

		TableColumn tableColumn_2 = new TableColumn(table_pageAction, SWT.NONE);
		tableColumn_2.setWidth(100);
		tableColumn_2.setText("分页数");

		TableColumn tableColumn_3 = new TableColumn(table_pageAction, SWT.NONE);
		tableColumn_3.setWidth(100);
		tableColumn_3.setText("数据模式");

		TableColumn tableColumn_4 = new TableColumn(table_pageAction, SWT.NONE);
		tableColumn_4.setWidth(100);
		tableColumn_4.setText("附加参数");

		Button button = new Button(composite, SWT.NONE);
		button.setBounds(714, 467, 80, 27);
		button.setText("应用");

		Button button_1 = new Button(composite, SWT.NONE);
		button_1.setBounds(628, 467, 80, 27);
		button_1.setText("保存");

		tbtext = new TabItem(tabFolder, SWT.NONE);
		tbtext.setText("文本");

		Composite composite_2 = new Composite(tabFolder, SWT.NONE);
		tbtext.setControl(composite_2);

		Label label_1 = new Label(composite_2, SWT.NONE);
		label_1.setBounds(86, 44, 51, 17);
		label_1.setText("访问间隔:");

		text_accessInterval = new Text(composite_2, SWT.BORDER);
		text_accessInterval.setBounds(143, 41, 104, 23);

		Label lblS = new Label(composite_2, SWT.NONE);
		lblS.setBounds(253, 44, 61, 17);
		lblS.setText("s");

		Label label_2 = new Label(composite_2, SWT.NONE);
		label_2.setBounds(98, 73, 39, 17);
		label_2.setText("优先级:");

		combo_yxj = new Combo(composite_2, SWT.READ_ONLY);
		combo_yxj.setItems(new String[] { "0", "1", "2", "3", "4" });
		combo_yxj.setBounds(143, 70, 88, 25);
		combo_yxj.select(0);

		text_cycleLength = new Text(composite_2, SWT.BORDER);
		text_cycleLength.setBounds(143, 101, 104, 23);

		Label label_3 = new Label(composite_2, SWT.NONE);
		label_3.setBounds(110, 104, 27, 17);
		label_3.setText("周期:");

		text_cycleCount = new Text(composite_2, SWT.BORDER);
		text_cycleCount.setBounds(143, 130, 104, 23);

		Label lblNewLabel_2 = new Label(composite_2, SWT.NONE);
		lblNewLabel_2.setBounds(86, 133, 51, 17);
		lblNewLabel_2.setText("循环次数:");

		Label label_4 = new Label(composite_2, SWT.NONE);
		label_4.setBounds(62, 162, 75, 17);
		label_4.setText("过滤窗口大小:");

		text_slidingWindowSize = new Text(composite_2, SWT.BORDER);
		text_slidingWindowSize.setBounds(143, 159, 104, 23);

		combo_iflog = new Combo(composite_2, SWT.READ_ONLY);
		combo_iflog.setItems(new String[] { "否", "是" });
		combo_iflog.setBounds(143, 188, 88, 25);
		combo_iflog.select(1);

		Label label_5 = new Label(composite_2, SWT.NONE);
		label_5.setBounds(86, 191, 51, 17);
		label_5.setText("是否登录:");

		missionTable = new Table(shlAsp, SWT.BORDER | SWT.FULL_SELECTION);
		missionTable.setBounds(10, 75, 253, 318);
		missionTable.setHeaderVisible(true);
		missionTable.setLinesVisible(true);
		missionTable.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent e) {
				if (e.button == 3) {
					Menu menu = new Menu(missionTable);
					missionTable.setMenu(menu);
					MenuItem item = new MenuItem(menu, SWT.PUSH);
					item.setText("删除");
					item.addListener(SWT.Selection, new Listener() {
						public void handleEvent(Event event) {
							TableItem item1 = (TableItem) missionTable.getSelection()[0];
							Table parent = item1.getParent();
							if (parent != null) {
								missionTable.remove(missionTable.getSelectionIndices());
							}
						}
					});
				}
				if (e.button == 1) {
					int selectIndex = missionTable.getSelectionIndex();
					if (selectIndex > -1) {
						TableItem tbItem = missionTable.getItem(selectIndex);
						String taskId = tbItem.getText(0);
						MissionService ms = new MissionService();
						String json = ms.getMissionListByTaskId(taskId);
						setPageAction(json);
					}
				}
			}
		});

		TableColumn tblclmnId_1 = new TableColumn(missionTable, SWT.NONE);
		tblclmnId_1.setWidth(35);
		tblclmnId_1.setText("id");

		TableColumn tblclmnNewColumn = new TableColumn(missionTable, SWT.NONE);
		tblclmnNewColumn.setWidth(49);
		tblclmnNewColumn.setText("名称");

		TableColumn tblclmnNewColumn_1 = new TableColumn(missionTable, SWT.NONE);
		tblclmnNewColumn_1.setWidth(57);
		tblclmnNewColumn_1.setText("信源");

		TableColumn tblclmnNewColumn_2 = new TableColumn(missionTable, SWT.NONE);
		tblclmnNewColumn_2.setWidth(63);
		tblclmnNewColumn_2.setText("方式");

		TableColumn tblclmnNewColumn_3 = new TableColumn(missionTable, SWT.NONE);
		tblclmnNewColumn_3.setWidth(73);
		tblclmnNewColumn_3.setText("状态");

		Label lblNewLabel = new Label(shlAsp, SWT.NONE);
		lblNewLabel.setBounds(10, 52, 61, 17);
		lblNewLabel.setText("任务列表");

		Label lblNewLabel_1 = new Label(shlAsp, SWT.NONE);
		lblNewLabel_1.setBounds(10, 399, 61, 17);
		lblNewLabel_1.setText("统计信息");

		table_1 = new Table(shlAsp, SWT.BORDER | SWT.FULL_SELECTION);
		table_1.setBounds(10, 422, 253, 187);
		table_1.setHeaderVisible(true);
		table_1.setLinesVisible(true);

		TableColumn tblclmnNewColumn_4 = new TableColumn(table_1, SWT.NONE);
		tblclmnNewColumn_4.setWidth(100);
		tblclmnNewColumn_4.setText("总访问页面数");

		TableColumn tblclmnNewColumn_5 = new TableColumn(table_1, SWT.NONE);
		tblclmnNewColumn_5.setWidth(100);
		tblclmnNewColumn_5.setText("New Column");

		Label label = new Label(shlAsp, SWT.NONE);
		label.setBounds(269, 52, 61, 17);
		label.setText("任务明细");

	}

	public void initService() {
		display.asyncExec(new Runnable() {
			public void run() {
				MissionService ms = new MissionService();
				List<Map<String, Object>> lm = ms.getMissionList();
				for (Map<String, Object> map : lm) {
					if (map.get("duplicate").toString().equals("0") ) {
						continue;
					}
					if (StringUtil.isNullOrEmpty(map.get("taskName").toString())) {
						continue;
					}
					System.err.println(StringUtil.mapToJson(map));
					TableItem item = new TableItem(missionTable, SWT.NONE);
					String type = "";
					if ("1".equals(map.get("taskStatus").toString())) {
						type = "正常";
					} else {
						type = "停止";
					}
					item.setText(new String[] { map.get("taskId").toString(), map.get("taskName").toString(),
							StringUtil.jsonToMap(map.get("source").toString()).get("webName").toString(),
							map.get("cycleLength").toString(), type });
					if ("正常".equals(type)) {
						item.setBackground(display.getSystemColor(SWT.COLOR_GREEN));// 设置行背景色
					} else {
						item.setBackground(display.getSystemColor(SWT.COLOR_RED));// 设置行背景色
					}
				}
			}
		});
	}

	public void setPageAction(final String lm) {
		display.asyncExec(new Runnable() {
			@SuppressWarnings("unchecked")
			public void run() {
				table_pageAction.removeAll();
				Map<String, Object> jsonMap = StringUtil.jsonToMap(lm);
				List<Map<String, Object>> infoClassMapList = (List<Map<String, Object>>) jsonMap
						.get("infoClassMapList");
				for (Map<String, Object> map : infoClassMapList) {
					System.out.println(StringUtil.mapToJson(map));
					TableItem item = new TableItem(table_pageAction, SWT.NONE);
					item.setText(new String[] { map.get("classinfoID").toString(), map.get("actionName").toString(),
							map.get("className").toString(), map.get("pageNumber").toString(), "NULL",
							map.get("addtionalParameter").toString() });
				}
				table_init.removeAll();
				List<Map<String, Object>> taskEntryList = (List<Map<String, Object>>) jsonMap.get("taskEntryList");
				for (Map<String, Object> map : taskEntryList) {
					TableItem item = new TableItem(table_init, SWT.NONE);
					item.setText(new String[] { map.get("entryID").toString(), map.get("entryType").toString(),
							map.get("entryUrl").toString(), "" });
				}

				String islog = jsonMap.get("accesslogin").toString();
				int islogCheck = Integer.parseInt(islog);
				combo_iflog.select(islogCheck);
				int yxj = Integer.parseInt(jsonMap.get("priority").toString());
				combo_yxj.select(yxj);
				int cycleLength = Integer.parseInt(jsonMap.get("cycleLength").toString());
				text_cycleLength.setText(cycleLength + "");
				int accessInterval = Integer.parseInt(jsonMap.get("accessInterval").toString());
				text_accessInterval.setText(accessInterval + "");
				int cycleCount = Integer.parseInt(jsonMap.get("cycleCount").toString());
				text_cycleCount.setText(cycleCount + "");
				int slidingWindowSize = Integer.parseInt(jsonMap.get("slidingWindowSize").toString());
				text_slidingWindowSize.setText(slidingWindowSize + "");
			}
		});
	}

}
